/**
 * Copyright:Copyright(c)2014-2017
 * Company:厦门市易致达物联网科技有限公司
 * @version 1.0
 */
package shop.controller.manage.product;
import shop.core.common.bean.JSONResult;
import shop.core.util.KeyValueHelper;
import shop.core.Services;
import shop.core.util.MD5;
import shop.core.util.WriteToHtmlUtils;
import shop.services.manage.product.ProductService;
import shop.services.manage.product.bean.Product;
import shop.core.BaseController;
import shop.core.RequestHolder;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**   
 * @类名称：ProductAction      
 * @创建人：wzl
 * @创建时间：2018-05-09 下午19:24:05      
 * @版本号：1.0
 * @描述：     
 */
@Controller
@RequestMapping("/manage/product/")
public class ProductAction extends BaseController<Product> {
    private static final Logger logger = LoggerFactory.getLogger(ProductAction.class);
    @Autowired
    private ProductService productService;
    private static final String page_toList = "/manage/product/productList";
    private static final String page_toAdd = "/manage/product/productAdd";
    private static final String page_toEdit = "/manage/product/productEdit";
    public ProductService getService() {
        return productService;
    }

    private ProductAction(){
        super.page_toList = page_toList;
        super.page_toAdd = page_toAdd;
        super.page_toEdit = page_toEdit;
    }

    @RequestMapping(value = "insertJson", method = RequestMethod.POST)
    @ResponseBody
    @Override
    public JSONResult insertJson(HttpServletRequest request, @ModelAttribute("e")Product e) throws Exception {
        jsonResult = new JSONResult();
        int id=getService().insert(e);
        e= getService().selectOne(e);
        Map data = new HashMap();
        data.put("o",e);
        WriteToHtmlUtils.CreateHtmlFile(request,"cpxq.ftl",data,"/cpzx/cpxl/"+id);
        return jsonResult;
    }

    @RequestMapping(value = "createHtmlJson", method = RequestMethod.POST)
    @ResponseBody
    public JSONResult createHtmlJson(HttpServletRequest request) throws Exception {
        jsonResult = new JSONResult();
        List<Product> allList= getService().selectList(new Product());
        if(allList.size()==0){
            return  jsonResult;
        }

        for (int i = 0 ; i < allList.size() ; i++) {
            Map data = new HashMap();
            Product e = getService().selectOne(allList.get(i));
            data.put("o",e);
            WriteToHtmlUtils.CreateHtmlFile(request,"cpxq.ftl",data,"/cpzx/cpxl/"+e.getId());
        }
        return jsonResult;
    }



}
